<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Config;

/**
 * 控制台
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{
    /**
     * 查看
     */
    public function index()
    {
        $user_model = new \app\admin\model\User;

        $sms_model = new \app\common\model\Sms;
        $user_token_model = new \app\admin\model\UserToken;
        $order_model = new \app\admin\model\order\Order;
        // var_dump(ORG_CATE_ID);die;
        $org_cate_id = ORG_CATE_ID;
        // 增加对单位的限制
        if($org_cate_id != 0){
            $where_user['org_cate_id'] = $org_cate_id;
        }

        $this->view->assign(['org_cate_id'=>$org_cate_id]);
        // 查询用户总量
        $where_user['user_auth_type'] = 0;
        $user_total_list = $user_model->where($where_user)->select();
        $user_total_list = collection($user_total_list)->toArray();
        //       查询用户短信的消耗
        $sms_num = 0;
        foreach ($user_total_list as $item) {
            if($item['mobile']){
                $where_user_sms_count['mobile'] = $item['mobile'];
                $sms_num += $sms_model->where($where_user_sms_count)->count();
            }

        }
        $this->view->assign(['sms_num'=>$sms_num]);
        // 查询当前单位所有的活跃数
        $user_string = '';
        $reg_user_count = 0;
        $visitor_user_count = 0;
        $visitor_user_string = '';
        $reg_user_string = '';

        $user_visitor_string = '';
        foreach ($user_total_list as $k => $v) {
            if($v['user_type'] == 2){
                $visitor_user_count++;
                $visitor_user_string .= $v['id'].',';
            }else{
                $reg_user_count++;
                $reg_user_string .= $v['id'].',';
            }
            $user_string .= $v['id'].',';
        }

        // 处理用户查询的图表日期
        $start_time_string = $this->request->param('starttime');
        $end_time_string = $this->request->param('endtime');
        $start_time_string = $start_time_string?$start_time_string:date('Y-m-d',strtotime('-7 day'));
        $end_time_string = $end_time_string?$end_time_string:date('Y-m-d');
        $this->view->assign([
            'start_time_string'        => $start_time_string,
            'end_time_string'        => $end_time_string
        ]);
        $start_time_int = strtotime($start_time_string);
        $end_time_int =  strtotime($end_time_string);
        $user_content_time_list = [];
        // 限制可用查询日期
        $active_user_count_list = [];
        $active_reg_user_count_list = [];
        $active_visitor_user_count_list = [];
        $where_active_user_total['user_id'] = ['IN',$user_string];
        $where_active_user_total['createtime'] = ['GT',$start_time_int-60*60*24*7];
        $active_user_total = $user_token_model->where($where_active_user_total)->count();
        
        // var_dump($user_reg_string);var_dump($visitor_user_string);die;
        for ($i=0; $i < 7;$i++) {
            if($end_time_int > $start_time_int && count($user_content_time_list ) < 7){
                $user_content_time_list[] = date('Y-m-d',$end_time_int);
                $end_time_ints = $end_time_int+60*60*24;
                // 查询注册用户活跃数
                $where_reg_user['user_id'] = ['IN',$reg_user_string];
                $active_reg_user_count_list[] = $active_reg_user_count = $user_token_model->group("user_id")->where("createtime >= $end_time_int AND createtime <= $end_time_ints")->where($where_reg_user)->count();
                // 查询访客用户活跃数
                $where_visitor_user['user_id'] = ['IN',$visitor_user_string];
                $active_visitor_user_count_list[] = $active_visitor_user_count = $user_token_model->group("user_id")->where("createtime >= $end_time_int AND createtime <= $end_time_ints")->where($where_visitor_user)->count();
                $active_user_count_list[] = $active_reg_user_count+$active_visitor_user_count;
            }else{
                break;
            }
            $end_time_int -= 60*60*24;
        }
        // 查询普通订单数量
        $where_order_num['type'] = 1;
        // 增加对单位的限制
        if($org_cate_id != 0){
            $where_order_num['order.org_cate_id'] = $org_cate_id;
        }

        $order_num = $order_model
                ->with(["course","user"])
                ->where($where_order_num)
                ->where(['course.is_chat'=>0])
                ->count();
         $chat_order_num = $order_model
                ->with(["course","user"])
                ->where($where_order_num)
                ->where(['course.is_chat'=>1])
                ->count();


        // var_dump($active_reg_user_count_list);die;
        $this->view->assign([
            'chat_order_num'        => $chat_order_num,
            'order_num'        => $order_num,
            'reg_user_count'        => $reg_user_count,
            'visitor_user_count'        => $visitor_user_count,
            'user_content_time_list'        => $user_content_time_list,
            'active_reg_user_count_list'        => $active_reg_user_count_list,
            'active_visitor_user_count_list'        => $active_visitor_user_count_list,
            'active_user_count_list'        => $active_user_count_list,
            'user_total'        => count($user_total_list),
            'active_user_total'       => $active_user_total
        ]);
        /**
         * 平台数据
         */
        // 查询平台注册单位数
        if($org_cate_id != 0){
            $where_reg_org_num['id'] = $org_cate_id;
        }
        $where_reg_org_num['parent_id'] = 0;
        $where_reg_org_num['level'] = 1;
        $org_cate_model = new \app\admin\model\org\Orgcate;
        // var_dump($active_user_total);die;
        $reg_org_num = $org_cate_model->where($where_reg_org_num)->count();
        $this->view->assign([
            'reg_org_num'        => $reg_org_num
        ]);





        return $this->view->fetch();
    }

    /**
     * 查看
     */
    public function index_bak()
    {
        $seventtime = \fast\Date::unixtime('day', -7);
        $paylist = $createlist = [];
        for ($i = 0; $i < 7; $i++)
        {
            $day = date("Y-m-d", $seventtime + ($i * 86400));
            $createlist[$day] = mt_rand(20, 200);
            $paylist[$day] = mt_rand(1, mt_rand(1, $createlist[$day]));
        }
        $hooks = config('addons.hooks');
        $uploadmode = isset($hooks['upload_config_init']) && $hooks['upload_config_init'] ? implode(',', $hooks['upload_config_init']) : 'local';
        $addonComposerCfg = ROOT_PATH . '/vendor/karsonzhang/fastadmin-addons/composer.json';
        Config::parse($addonComposerCfg, "json", "composer");
        $config = Config::get("composer");
        $addonVersion = isset($config['version']) ? $config['version'] : __('Unknown');
        $this->view->assign([
            'totaluser'        => 35200,
            'totalviews'       => 219390,
            'totalorder'       => 32143,
            'totalorderamount' => 174800,
            'todayuserlogin'   => 321,
            'todayusersignup'  => 430,
            'todayorder'       => 2324,
            'unsettleorder'    => 132,
            'sevendnu'         => '80%',
            'sevendau'         => '32%',
            'paylist'          => $paylist,
            'createlist'       => $createlist,
            'addonversion'       => $addonVersion,
            'uploadmode'       => $uploadmode
        ]);

        return $this->view->fetch();
    }

}
